package com.zy.socket.testfileUpcase;

import java.io.*;
import java.net.Socket;

/**
 * @Author Gong
 * @Description 服务器把文件转大写实现
 * @dATE 2021/12/19 14:44
 **/
public class Client {
    public static void main(String[] args) {
        OutputStream outputStream =null;
        FileInputStream fileInputStream =null;
        FileOutputStream fileOutputStream =null;
        try {
             Socket socket = new Socket("127.0.0.1", 9999);

             //给服务器发送文本
             outputStream = socket.getOutputStream();
             fileInputStream = new FileInputStream(new File("d:/temp/test.txt"));
            byte[] buffer = new byte[1024];
            int len;
            while((len = fileInputStream.read(buffer)) > -1){
                outputStream.write(buffer,0,len);
            }
            //告知服务端文件发送完毕
            socket.shutdownOutput();
            //等待服务器回数据
            //存文件
             fileOutputStream = new FileOutputStream(new File("d:/temp/test_xxx.txt"));
            InputStream inputStream = socket.getInputStream();
            byte[] bufferIn = new byte[1024];
            int leng;
            while ((leng= inputStream.read(bufferIn)) > -1){
//                String str = new String(bufferIn,0,leng);
//                System.out.print(str);
                fileOutputStream.write(bufferIn,0,leng);
            }
            System.out.println("客户端接收数据完毕。");

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileInputStream.close();
                outputStream.close();
                fileOutputStream.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}
